
#include "AnchorHandle.h"
#include "../PluginInterface/ITool.h"

AnchorHandle::AnchorHandle(int id, ITool *parent)
    : QGraphicsItem((QGraphicsItem*)parent, NULL), _parent(parent), _id(id)
{
    _anchor.load(":/libkanvas/images/anchor_image.png");
    setVisible(false);
}

AnchorHandle::~AnchorHandle()
{

}

void AnchorHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
    int half_anchor_size = _anchor.width() / 2;

    painter->drawPixmap(pos(),_anchor);
}

void AnchorHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
}

void AnchorHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    _parent->handleAnchorMove(this, event);
    QGraphicsItem::mouseMoveEvent(event);
}

void AnchorHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
}

void AnchorHandle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    // change the cursor
    _currentCursor = scene()->views().at(0)->cursor();
}

void AnchorHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    scene()->views().at(0)->setCursor(_currentCursor);
}

QRectF AnchorHandle::boundingRect() const
{
    qreal half_anchor_size = (qreal)_anchor.width() / 2.0;
    qreal x = scenePos().x() - half_anchor_size;
    qreal y = scenePos().y() - half_anchor_size;
    return QRectF(x, y, _anchor.width()+half_anchor_size, _anchor.height()+half_anchor_size);
}
